/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: NewsActivity
 * Author: Clark
 * Date: 2020/9/30 9:34
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
package sz.szsmk.citizencard.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chenenyu.router.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.NewsActivityBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonFragmentComponent;
import sz.szsmk.citizencard.model.network.bean.response.NoticeListRes;
import sz.szsmk.citizencard.model.network.bean.response.NoticeMainres;
import sz.szsmk.citizencard.mvp.contract.INewsContact;
import sz.szsmk.citizencard.mvp.presenter.NewsPresenter;
import sz.szsmk.citizencard.ui.adapter.NewsOrderAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * @ClassName: NewsActivity
 * @Description:
 * @Author: Clark
 * @Date: 2020/9/30 9:34
 */
@Route("HomeNews")
public class NewsActivity extends IBaseActivity<NewsPresenter, NewsActivityBinding>
        implements INewsContact.INewsView, View.OnClickListener{

    private NewsOrderAdapter newsOrderAdapter;
    private SwipeRefreshLayout shopingSwiperefresh;
    private List<NoticeMainres.MainPageInfoListBean> infoList = new ArrayList<>();
    private List<String> zdList = new ArrayList<>();
    private List<String> htList = new ArrayList<>();
    private List<NoticeMainres.MainPageInfoListBean> overList = new ArrayList<>();
    private List<NoticeMainres.MainPageInfoListBean> showList;
    private List<NoticeMainres.MainPageInfoListBean> mList = new ArrayList<>();
    private NoticeMainres.MainPageInfoListBean bean;

    @Override
    public void getMainNoticeError(String message) {
        dataBinding.swipeRefreshLayout.finishLoadMore();
        dataBinding.swipeRefreshLayout.finishRefresh();
    }

    @Override
    public void getMainNoticeSucc(List<NoticeMainres.MainPageInfoListBean> mainPageInfoList) {
        infoList.clear();
        infoList.addAll(mainPageInfoList);
        newsOrderAdapter.setItemData(infoList);
        newsOrderAdapter.notifyDataSetChanged();
        dataBinding.swipeRefreshLayout.finishLoadMore();
        dataBinding.swipeRefreshLayout.finishRefresh();
    }

    @Override
    public void queryAndSearchSucc(List<NoticeListRes.MessageDetailListBean> detailList) {

    }


    @Override
    protected void initSystemBar(){
     //   super.initSystemBar();
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(false, 0.0f)
                .statusBarColor(R.color.color_2184FA).init();
    }


    @Override
    public void queryAndSearchFail(NoticeListRes response) {

    }

    @Override
    public void delItemSucc() {
        mPresenter.getMainNotice();
    }

    @Override
    public void delItemFail(String message) {

    }
    public void removeData(int position) {
        infoList.remove(position);
//        删除动画
        newsOrderAdapter.notifyItemRemoved(position);
        newsOrderAdapter.notifyDataSetChanged();
    }
    @Override
    protected int attachLayoutRes() {
        return R.layout.news_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        zdList = new ArrayList<>();
        overList = new ArrayList<>();
        showList = new ArrayList<>();
        dataBinding.noticeSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    String word = dataBinding.noticeSearch.getText().toString();
                    if (TextUtils.isEmpty(word)) {
                        PubUtils.popTipOrWarn(NewsActivity.this, "请输入搜索内容");
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putString("word", word);
                        bundle.putString("title", "搜索结果");
                        JumpUtils.jumptoNatvie(NewsActivity.this, "noticelist", 0, bundle);
                    }
                    return true;
                }
                return false;
            }
        });
        dataBinding.swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getMainNotice();
            }
        });
        dataBinding.swipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getMainNotice();
            }
        });

        dataBinding.titleLayout.back.setOnClickListener(NewsActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(NewsActivity.this);
        dataBinding.newsRv.setLayoutManager(linearLayoutManager);
        newsOrderAdapter = new NewsOrderAdapter(NewsActivity.this);
        newsOrderAdapter.setPopReleaseCallBack(new NewsOrderAdapter.ReleaseCallBack() {
            @Override
            public void onPopReleaseCallBack(String messageType) {
                mPresenter.changeIsRead(messageType, "1");
            }
        });
        newsOrderAdapter.setUpItemCallBack(new NewsOrderAdapter.UpitemCallBack() {
            @Override
            public void onUpItemCallBack(int position) {
            }
        });
        newsOrderAdapter.setDelItemCallBack(new NewsOrderAdapter.DelitemCallBack() {
            @Override
            public void onDelItemCallBack(int position) {
                NoticeMainres.MainPageInfoListBean mainPageInfoListBean = infoList.get(position);
                String messageType = mainPageInfoListBean.getMessageType();
                mPresenter.delItem(messageType);
                removeData(position);
            }
        });
        dataBinding.newsRv.setAdapter(newsOrderAdapter);

    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getMainNotice();
//        ((IMainActivity) getActivity()).getFragment(0);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        dataBinding.noticeSearch.clearFocus();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            finish();
        }
    }
}